package com.zzh.lib.drawable;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import static com.zzh.lib.views.utils.DimenUtil.sp2px;

/**
 * Created by ZZH on 2020/10/10.
 *
 * @Date: 2020/10/10
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: 水印
 */
public class WaterMarkDrawable extends Drawable {
    private Paint paint = new Paint();
    private List<String> labels;
    private Context context;
    private int degree = 30;    // 角度
    private int fontSize = 14;   // 字体大小 单位sp
    private int lines = 15;
    private String textColor = "#989898";
    private int textIntColor = Color.parseColor("#989898");

    public List<String> getLabels() {
        if (labels == null) {
            return new ArrayList<>();
        }
        return labels;
    }

    public int getLines() {
        return lines;
    }

    public WaterMarkDrawable setLines(int lines) {
        if (lines > 0) {
            this.lines = lines;
        }
        return this;
    }

    public WaterMarkDrawable setLabels(List<String> labels) {
        this.labels = labels;
        return this;
    }

    public int getDegree() {
        return degree;
    }

    public WaterMarkDrawable setDegree(int degree) {
        this.degree = degree;
        return this;
    }

    public int getFontSize() {
        return fontSize;
    }

    public WaterMarkDrawable setFontSize(int fontSize) {
        this.fontSize = fontSize;
        return this;
    }

    public String getTextColor() {
        return textColor == null ? "#989898" : textColor;
    }

    public WaterMarkDrawable setTextColor(String textColor) {
        this.textColor = textColor;
        return this;
    }

    public int getTextIntColor() {
        if (textIntColor == 0) {
            return textIntColor = Color.parseColor(getTextColor());
        }
        return textIntColor;
    }

    public WaterMarkDrawable setTextIntColor(int textIntColor) {
        this.textIntColor = textIntColor;
        return this;
    }

    public WaterMarkDrawable(Context context) {
        this.context = context;
    }

    /**
     * 初始化构造
     *
     * @param context  上下文
     * @param labels   水印文字列表 多行显示支持
     * @param degree   水印角度
     * @param fontSize 水印文字大小
     */
    public WaterMarkDrawable(Context context, List<String> labels, int degree, int fontSize) {
        this.labels = labels;
        this.context = context;
        this.degree = degree;
        this.fontSize = fontSize;
    }

    @Override
    public void draw(@NonNull Canvas canvas) {

        if (getLabels().isEmpty()) {
        } else {
            int width = getBounds().right;
            int height = getBounds().bottom;
            // 画布背景色
            canvas.drawColor(Color.TRANSPARENT);
            // 水印文字的颜色
            paint.setColor(getTextIntColor());
            paint.setAlpha(40);
            paint.setAntiAlias(true);
            paint.setTextSize(sp2px(context, fontSize));
            canvas.save();
            canvas.rotate(degree);
            float textWidth = paint.measureText(labels.get(0));
            int index = 0;
            for (int positionY = height / lines; positionY <= height; positionY += height / lines + 50) {
                float fromX = -width + (index++ % 2) * textWidth;
                for (float positionX = fromX; positionX < width; positionX += textWidth * 2) {
                    int spacing = 0;//间距
                    for (String label : labels) {
                        canvas.drawText(label, positionX, positionY + spacing, paint);
                        spacing = spacing + lines;
                    }
                }
            }
            canvas.restore();
        }
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }
}
